﻿using System;
using System.Data.SqlClient;
using DataAccess;
using Entities;
using Common.Extentions;

namespace Business
{
    public class UserBusiness : IUserBusiness
    {
        /// <summary>
        /// Validates the user.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public bool ValidateUser(string account, string password)
        {
            if (String.IsNullOrEmpty(account) || String.IsNullOrEmpty(password)) return false;
            var userDataAccess = new UserDataAccess();
            password = Crypt.Encrypt(password);
            var user = userDataAccess.GetUser(account, password);
            return user != null;
        }

        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public User GetUser(string account, string password)
        {
            if (String.IsNullOrEmpty(account) || String.IsNullOrEmpty(password)) return null;
            var userDataAccess = new UserDataAccess();
            password = Crypt.Encrypt(password);
            return userDataAccess.GetUser(account, password);
        }
        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <returns></returns>
        public User GetUser(string account)
        {
            if (String.IsNullOrEmpty(account)) return null;
            var userDataAccess = new UserDataAccess();
            return userDataAccess.GetUser(account);
        }
        /// <summary>
        /// Gets the user by id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public User GetUserById(string userId)
        {
            var userDataAccess = new UserDataAccess();
            if (!string.IsNullOrEmpty(userId))
            {  try
                {
                    return userDataAccess.GetUserById(userId);
                }
                catch (SqlException)
                {

                    return null;
                }
            }

            return null;
        }

        /// <summary>
        /// Gets the password.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <returns></returns>
        private string GetPassword(string account)
        {
            if (String.IsNullOrEmpty(account)) return null;
            var userDataAccess = new UserDataAccess();
            var password = userDataAccess.GetPassword(account);
            if (password == null) return null;
            password = Crypt.Decrypt(password);
            return password;
        }

        /// <summary>
        /// Validates the email.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <param name="email">The email.</param>
        /// <returns></returns>
        public bool ValidateEmail(string account, string email)
        {
            if (String.IsNullOrEmpty(account) || String.IsNullOrEmpty(email)) return false;
            var userDataAccess = new UserDataAccess();
            var realEmail = userDataAccess.GetEmail(account);
            return email == realEmail;
        }

        /// <summary>
        /// Forgots the password.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <param name="email">The email.</param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool ForgotPassword(string account, string email, out string message)
        {
            message = "";
            if (String.IsNullOrEmpty(account) || String.IsNullOrEmpty(email)) return false;
            var currentPassword = GetPassword(account);
            if (currentPassword == null) return false;
            string body = "You received this message because you used forgot password function on the Services Directory Website.";
            body += "\nYour current password is: " + currentPassword;
            body += "\nThank you!";
            message = body;
            //Mail.Send(email,body);
            return true;
        }

        /// <summary>
        /// Forgots the password.
        /// </summary>
        /// <param name="account">The account.</param>
        /// <param name="email">The email.</param>
        /// <returns></returns>
        public bool ForgotPassword(string account, string email)
        {
            if (String.IsNullOrEmpty(account) || String.IsNullOrEmpty(email)) return false;
            var currentPassword = GetPassword(account);
            if (currentPassword == null) return false;
            string body = "You received this message because you used forgot password function on the Services Directory Website.";
            body += "\nYour current password is: " + currentPassword;
            body += "\nThank you!";
            //Mail.Send(email,body);
            return true;
        }
    }
}
